//
//  QWUploadConfig.m
//  WJUpload-master
//
//  Created by 吴云海 on 15-12-27.
//  Copyright (c) 2015年 WJ. All rights reserved.
//

#import "QWUploadConfig.h"
#import "NSDictionary+QWExtension.h"

@interface QWUploadConfig ()
@property (nonatomic, strong) NSMutableDictionary *us;
@property (nonatomic, strong) NSMutableDictionary *rus;
@property (nonatomic, strong) NSMutableDictionary *dps;
@property (nonatomic, strong) NSMutableDictionary *dss;
@end

@implementation QWUploadConfig

DEF_SINGLETON_INIT(QWUploadConfig)

-(void) singleInit {
    self.us = [[NSMutableDictionary alloc] init];
    self.rus = [[NSMutableDictionary alloc] init];
    self.dps = [[NSMutableDictionary alloc] init];
    self.dss = [[NSMutableDictionary alloc] init];
}

-(void)setUploadHandlers:(NSDictionary *)uploadHandlers {
    [uploadHandlers qw_each:^(id key, id value) {
        if ([key isKindOfClass:[NSNumber class]]) {
            if ([value conformsToProtocol:@protocol(IQWUploadHandler)]) {
                [self.us setObject:value forKey:key];
            } else {
                NSString *reason = [NSString stringWithFormat:@"%@ 必须实现协议 IQWUploadHandler",NSStringFromClass([value class])];
                @throw [NSException exceptionWithName:@"QWUploadConfigException" reason:reason userInfo:nil];
                QWLogError(reason);
            }
        } else {
            NSString *reason = [NSString stringWithFormat:@"%@ 必须是NSNumber类型",NSStringFromClass([key class])];
            @throw [NSException exceptionWithName:@"QWUploadConfigException" reason:reason userInfo:nil];
            QWLogError(reason);
        }
    }];
}

-(void)setResultHandlers:(NSDictionary *)resultHandlers {
    [resultHandlers qw_each:^(id key, id value) {
        if ([key isKindOfClass:[NSNumber class]]) {
            if ([value conformsToProtocol:@protocol(IQWUploadResultHandler)]) {
                [self.rus setObject:value forKey:key];
            } else {
                NSString *reason = [NSString stringWithFormat:@"%@ 必须实现协议 IQWUploadResultHandler",NSStringFromClass([value class])];
                @throw [NSException exceptionWithName:@"QWUploadConfigException" reason:reason userInfo:nil];
                QWLogError(reason);
            }
        } else {
            
            NSString *reason = [NSString stringWithFormat:@"%@ 必须是NSNumber类型",NSStringFromClass([key class])];
            @throw [NSException exceptionWithName:@"QWUploadConfigException" reason:reason userInfo:nil];
            QWLogError(reason);
        }
    }];
}

-(void)setDataProviders:(NSDictionary *)dataProviders {
    [dataProviders qw_each:^(id key, id value) {
        if ([key conformsToProtocol:@protocol(IQWUploadDataProvider)]) {
            if ([value isKindOfClass:[NSNumber class]]) {
                [self.dps setObject:value forKey:key];
            } else {
                NSString *reason = [NSString stringWithFormat:@"%@ 必须是NSNumber类型",NSStringFromClass([value class])];
                @throw [NSException exceptionWithName:@"QWUploadConfigException" reason:reason userInfo:nil];
                QWLogError(reason);
            }
        } else {
            NSString *reason = [NSString stringWithFormat:@"%@ 必须实现协议 IQWUploadDataProvider",NSStringFromClass([value class])];
            @throw [NSException exceptionWithName:@"QWUploadConfigException" reason:reason userInfo:nil];
            QWLogError(reason);
        }
    }];
}

-(void)setDataStorages:(NSDictionary *)dataStorages {
    [dataStorages qw_each:^(id key, id value) {
        if ([key isKindOfClass:[NSNumber class]]) {
            if ([value conformsToProtocol:@protocol(IQWUploadDataStorage)]) {
                [self.dss setObject:value forKey:key];
            } else {
                NSString *reason = [NSString stringWithFormat:@"%@ 必须实现协议 IQWUploadDataStorage",NSStringFromClass([value class])];
                @throw [NSException exceptionWithName:@"QWUploadConfigException" reason:reason userInfo:nil];
                QWLogError(reason);
            }
        } else {
            
            NSString *reason = [NSString stringWithFormat:@"%@ 必须是NSNumber类型",NSStringFromClass([key class])];
            @throw [NSException exceptionWithName:@"QWUploadConfigException" reason:reason userInfo:nil];
            QWLogError(reason);
        }
    }];
}

@end
